﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VirtualOrg.DB;

namespace VirtualOrg.Web
{
    public partial class FormCreateUser : OboutInc.oboutAJAXPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public bool CheckUsername(string searchText)
        {
            bool result = true;
            using (DbStoreDataContext dataContext = new DbStoreDataContext())
            {
                int? records = dataContext.CheckUsernameExist(searchText);

                if (records.Value > 0)
                {
                    result = false;
                }
            }

            return result;
        }

        public bool CheckEmail(string searchText)
        {
            bool result = true;
            using (DbStoreDataContext dataContext = new DbStoreDataContext())
            {
                int? records = dataContext.CheckUserEmailExist(searchText);

                if (records.Value > 0)
                {
                    result = false;
                }
            }

            return result;
        }

        public string CreateUser(string name, string emailID, string birthday, string sex, string country, string location, string username, string password)
        {
            MessageBox msgbox = null;
            bool valid = true;
            if (!CheckEmail(emailID))
            {
                msgbox = new MessageBox("Email ID already registered with us.");
                valid = false;
            }

            if (!CheckUsername(username))
            {
                msgbox = new MessageBox("Username already registered with us.");
                valid = false;
            }

            if (valid)
            {
                using (DbStoreDataContext dataContext = new DbStoreDataContext())
                {
                    dataContext.CreateUser(name, username, password, emailID, Convert.ToDateTime(birthday), sex, country, location);
                    msgbox = new MessageBox("Account created successfully.");
                }
            }

            return msgbox.ReturnObject();
        }
    }
}